package org.cangku.controller;

import org.cangku.dao.KuCunDao;
import org.cangku.dao.PJShopInfoDao;
import org.cangku.dao.ShopInfoDao;
import org.cangku.dao.XsLinShiDao;
import org.cangku.entity.Shopinfo;
import org.cangku.pojo.PJshopInfo;
import org.cangku.service.PJShopInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/shop")
public class PJShopInfoController {
    @Autowired
    private PJShopInfoDao PJShopInfoDao;
    @Autowired
    private ShopInfoDao shopInfoDao;
    @Autowired
    private KuCunDao kuCunDao;
    @Autowired
    private XsLinShiDao xsLinShiDao;

    @RequestMapping("/getall")
    @ResponseBody
    public List<PJshopInfo> getAll(){
        return PJShopInfoDao.getAll();
    }

    /*模糊分页查询*/
    @RequestMapping("/query")
    @ResponseBody
   public Map<String,Object> queryMoHuFenYe(String name,int typeid,int offset,int limit){
        Map<String,Object> map = new HashMap<>();

        map.put("rows",PJShopInfoDao.query(name,typeid,offset,limit));
        map.put("total",PJShopInfoDao.getByNameCount(name,typeid));
        return map;
    }

    /*判断输入的商品名称是否存在*/
    @RequestMapping("/qinjin")
    @ResponseBody
    public Map<String,Object> queryNameByName(String name){
        Map<String,Object> map = new HashMap<String,Object>();

        map.put("i",shopInfoDao.isextent(name));
        return map;
    }

    /*添加商品信息*/
    @RequestMapping("/shopInsert")
    @ResponseBody
    public Map<String,Object> addShopInfo(Shopinfo shopinfo, int pn, int dianpu){
        /*if(shopinfo.getId() == 0){
            *//*根据商品名称判断是否存在该商品*//*
            int i = shopInfoDao.isextent(shopinfo.getsName());
            if(i>0){
                *//*不添加该商品*//*
                *//*修改总库存数量 先获得商品id*//*
                int id = shopInfoDao.getShopId(shopinfo.getsName());//获得商品id
                *//*根据商品id修改总库存数量*//*
                int stoknum = (int)shopinfo.getStockNum();
                shopInfoDao.updateStockNum(id,stoknum);
                *//*创建库存对象并赋值*//*
                Kucun kk = new Kucun();
                kk.setSid(id);
                kk.setSname(shopinfo.getsName());
                kk.setKpeci(shopinfo.getSpeci());
                kk.setTypeid(shopinfo.getTypeid());
                kk.setGoldMoney(shopinfo.getGoldMoney());
                kk.setSaleMoney(shopinfo.getSaleMoney());
                kk.setStockNum(shopinfo.getStockNum());
                kk.setCostMoney(shopinfo.getGoldMoney()*shopinfo.getStockNum());
                //long ldp = (long)dianpu;
                kk.setShopId(dianpu);

                *//*先判断是否有一样的库存*//*
                int kcCount = kuCunDao.kkInfoisextend(kk);
                if(kcCount>0){
                    int stock =(int) shopinfo.getStockNum();
                    kuCunDao.updateKuCunNum(stock,id,dianpu);
                }else{
                    kuCunDao.insertKucunInfo(kk);
                }

            }else{
                boolean is = shopInfoDao.addShopInfo(shopinfo); //添加该商品
                int id = shopInfoDao.getShopId(shopinfo.getsName());//获得商品id
                *//*创建库存对象并赋值*//*
                Kucun kk = new Kucun();
                kk.setSid(id);
                kk.setSname(shopinfo.getsName());
                kk.setKpeci(shopinfo.getSpeci());
                kk.setTypeid(shopinfo.getTypeid());
                kk.setGoldMoney(shopinfo.getGoldMoney());
                kk.setSaleMoney(shopinfo.getSaleMoney());
                kk.setStockNum(shopinfo.getStockNum());
                kk.setCostMoney(shopinfo.getGoldMoney()*shopinfo.getStockNum());
                kk.setShopId(dianpu);
                *//*先判断是否有一样的库存*//*
                int kcCount = kuCunDao.kkInfoisextend(kk);
                if(kcCount>0){
                    int stock =(int) shopinfo.getStockNum();
                    kuCunDao.updateKuCunNum(stock,id,dianpu);
                }else{
                    kuCunDao.insertKucunInfo(kk);
                }
            }

        }
        Map<String,Object> map = new HashMap<String,Object>();
        map.put("rows",pjShopInfoService.queryAll("",0,(pn-1)*5,5));
        map.put("total",pjShopInfoService.getByNameCount("",0));*/
        return null;
    }

    /*根据商品id获得商品信息*/
    @RequestMapping("/huoquShopInfo")
    @ResponseBody
    public List<Shopinfo> getinfoByid(int id){
        return shopInfoDao.getShopInfoById(id);
    }

    /*修改商品信息*/
    @RequestMapping("/shopUpdate")
    @ResponseBody
    public Map<String,Object> updateShopinfo(int id,String name,String guige,int typeid,double jinjia,double shoujia,int kucun,int danwei,int pn){
        System.out.println(id+"-"+name+"-"+guige+"-"+typeid+"-"+jinjia+"-"+shoujia+"-"+kucun+"-"+danwei);
       /* System.out.println("编号是："+id);
        System.out.println(shopinfo.getId()+"-"+shopinfo.getSName()+"-"+shopinfo.getSpeci()+"-"+shopinfo.getTypeid()+"-"+shopinfo.getGoldMoney()
                +"-"+shopinfo.getSaleMoney()+"-"+shopinfo.getStockNum()+"-"+shopinfo.getCompany()
        );*/
      /* Shopinfo sp = new Shopinfo();
       sp.setId(id);
       sp.setsName(name);
       sp.setSpeci(guige);
       sp.setTypeid(typeid);
       sp.setGoldMoney(jinjia);
       sp.setSaleMoney(shoujia);
       sp.setStockNum(kucun);
       sp.setCompany(danwei);
       boolean is = shopInfoDao.updateShopInfo(sp);
       System.out.println(is);
       Map<String,Object> map = new HashMap<String,Object>();
       map.put("rows",pjShopInfoService.queryAll("",0,(pn-1)*5,5));
       map.put("total",pjShopInfoService.getByNameCount("",0));*/
       return null;
    }

    /*删除*/
    @RequestMapping("/delById")
    @ResponseBody
    public Map<String,Object> deleteShopInfo(int id,int pn){
        boolean is = shopInfoDao.deleteShopInfo(id);
        System.out.println(is);
        Map<String,Object> map = new HashMap<String,Object>();
        map.put("rows",PJShopInfoDao.query("",0,(pn-1)*5,5));
        map.put("total",PJShopInfoDao.getByNameCount("",0));
        return map;
    }

    /*批量删除*/
    @RequestMapping("/plDel")
    @ResponseBody
    public Map<String,Object> deleteShopInfo(String ids,int pn){
        String[] id = ids.split(",");
        for(int i = 0;i<id.length;i++){
            shopInfoDao.deleteShopInfo(Integer.parseInt(id[i]));
        }
        Map<String,Object> map = new HashMap<String,Object>();
        map.put("rows",PJShopInfoDao.query("",0,(pn-1)*5,5));
        map.put("total",PJShopInfoDao.getByNameCount("",0));
        return map;
    }
}
